<?php
class Controller {

	protected $area = "";
	protected $layout = "";

	/**
	 * Constructor
	 * @return void
	 */
	public function __construct(){
		Core::$controllerID = Request::get('mod');
		Core::$actionID = Request::getAction("index");
	}

	/**
	 *  Format action name from URL parameter to method name
	 *  @param string $name action name from URL parameter
	 *  @return string method name
	 */
	public static function _formatName($name)
	{
		$name = str_replace(array('-','_','.'),' ', strtolower($name));
		$name = preg_replace("[^a-z0-9 ]",'',$name);
		return str_replace(' ','',ucwords($name));
	}

	/**
	 * Creates new controller instance by module and area
	 *
	 * @param string $area current area
	 * @param string $mod current module name
	 * @return Controller module controller instance
	 */
	static function factory($area, $mod)
	{
		if ($area=='')
			$area='visitor';

		$className = self::_formatName($mod)."_Controller_".ucfirst($area);
		$ctrl = new $className();
		$ctrl->setArea($area);
		return $ctrl;
	}
}